CC ?= musl-gcc

TARGET=sh
VERSION=0.1

PREFIX ?= /planck
BINDIR ?= $(PREFIX)/bin
MANDIR ?= $(PREFIX)/share/man/man1
CONFDIR ?= /planck/etc/$(TARGET)
INSTGRP ?= root
INSTFILES ?=
DISTFILES=$(SRCDIR) $(INSTFILES) man inc config.h Makefile CHANGELOG LICENSE README doc

SRCDIR=src

TARGET_CFLAGS ?= -Wall -g -pedantic -DTARGET=\"$(TARGET)\" -DVERSION=\"$(VERSION)\" -Iinc/ -I. $(CFLAGS)
TARGET_CPPFLAGS ?= $(CPPFLAGS)
TARGET_CLIBS ?= $(CLIBS)
TARGET_LDFLAGS ?= $(LDFLAGS)

OBJS=$(SRCDIR)/main.o $(SRCDIR)/env.o $(SRCDIR)/path.o $(SRCDIR)/parse.o $(SRCDIR)/lib.o $(SRCDIR)/builtin.o $(SRCDIR)/clist.o $(SRCDIR)/fork.o $(SRCDIR)/tty.o $(SRCDIR)/history.o $(SRCDIR)/tokenize.o $(SRCDIR)/tabfill.o

all: default

default: $(TARGET)

$(TARGET): $(OBJS)
	$(CC) $(TARGET_LDFLAGS) -o $(TARGET) $(OBJS) $(TARGET_CLIBS)

dist-base: distclean
	mkdir -p $(TARGET)-$(VERSION)
	cp -Rt $(TARGET)-$(VERSION) $(DISTFILES)

dist-gz: dist-base
	tar czf $(TARGET)-$(VERSION).tar.gz $(TARGET)-$(VERSION)
	rm -r $(TARGET)-$(VERSION)

dist-bz2: dist-base
	tar cjf $(TARGET)-$(VERSION).tar.bz2 $(TARGET)-$(VERSION)
	rm -r $(TARGET)-$(VERSION)

dist: dist-bz2

distclean:
	rm -f $(OBJS)

clean: distclean
	rm -f $(TARGET)

install: $(TARGET)
	install $(TARGET) $(BINDIR)/$(TARGET)
	install --group=$(INSTGRP) -d $(CONFDIR)
	-install --group=$(INSTGRP) -t $(CONFDIR) $(INSTFILES)
	-install man/$(TARGET).1 $(MANDIR)/$(TARGET).1

uninstall:
	rm -f $(BINDIR)/$(TARGET)

fresh: clean all

$(SRCDIR)/%.o: $(SRCDIR)/%.c inc/dish.h config.h
	$(CC) $(TARGET_CPPFLAGS) $(TARGET_CFLAGS) -o $@ -c $<

.PHONY: all default distclean dist dist-base dist-bz2 dist-gz clean fresh install uninstall
